package web.admin.shop;

import domain.ImageEntry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.admin.ShopAdminService;
import web.admin.shop.product.tabs.UploadPanel;

public final class ImportPanel extends Panel {

    private static final Log log = LogFactory.getLog(UploadPanel.class);
    @SpringBean
    private ShopAdminService shopAdminService;

    public ImportPanel(String id) {
        super(id);

        final FileUploadForm simpleUploadForm = new FileUploadForm("simpleUpload");
        add(simpleUploadForm);


    }

    private class FileUploadForm extends Form<Void> {

        private FileUploadField file;

        public FileUploadForm(String name) {
            super(name);

            // set this form to multipart mode (allways needed for uploads!)
            setMultiPart(true);

            // Add one file input field
            add(file = new FileUploadField("fileInput"));

            // Set maximum size to 100K for demo purposes
//            setMaxSize(Bytes.kilobytes(100));
        }

        @Override
        protected void onSubmit() {
            // A dao object that will hold information about the uploaded image.
            ImageEntry imageEntry = null;
            // The FileUpload object that will be provided by wicket that holds info about
            // the file uploaded to the webapp
            FileUpload fupload = file.getFileUpload();
            if (fupload == null) {
                // No image was provided
                error(getLocalizer().getString("upload.fail.onlyImage", this));
                return;
            } else if (fupload.getSize() == 0) {
                error(getLocalizer().getString("upload.fail.emptyImage", this));
                return;
            } else if (!checkContentType(fupload.getContentType())) {
                // checkContentType(String) said that they uploaded a file of the wrong type.                
                error(getLocalizer().getString("upload.fail.imageType", this));
                return;
            } else {
                // Create a new dao object and populate it with info about the image
//                File newFile = new File(fupload.getClientFileName());

                try {

//                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
//                     IOUtils.copy(fupload.getInputStream(), baos);
//                     baos.close();
//                      byte[] data = baos.toByteArray();
                    shopAdminService.importProductsFromXML(fupload.getInputStream());


//                } catch (DirectoryPathException e) {
//                    log.error(e);
//                    // It'd be nice to have a logger so that we could log the error.
//                    error(getLocalizer().getString("system.error.directory", this));
                } catch (Exception ioe) {
                    log.error(ioe);
                    // It'd be nice to have a logger so that we could log the error.
                    error(getLocalizer().getString("upload.fail.error", this));
                }
            }
        }
    }

    private static final String[] VALID_CONTENT_TYPES = {
            "text/xml", "xml"
    };

    private boolean checkContentType(String contentType) {
        for (int i = 0; i < VALID_CONTENT_TYPES.length; i++) {
            if (VALID_CONTENT_TYPES[i].equalsIgnoreCase(contentType)) {
                return true;
            }
        }
        return false;
    }
}
